package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rivercloud.dao.UserProjectDao;
import com.rivercloud.entity.Project;
import com.rivercloud.entity.UserProject;

public class UserProjectDaoImpl implements UserProjectDao{

	 static SqlSessionFactory sqlSessionFactory = null;
	 static{
		 SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		 InputStream inputStream =null;
		 
		 try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	 }
	
	
	@Override
	public void addUserProject(UserProject userProject) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("addUserProject",  userProject);
		session.commit();
		session.close();
		
	}




	@Override
	public void deleteMember(Integer projectId, Integer userId) {
		SqlSession session = sqlSessionFactory.openSession();
		UserProject userProject = new UserProject();
		userProject.setProjectId(projectId);
		userProject.setUserId(userId);
		session.delete("deleteMember",userProject);
		session.commit();
		session.close();
		
	}
	

	@Override
	public List<Integer> findUserIdsByProjectId(int projectId) {
		SqlSession session = sqlSessionFactory.openSession();
		
		List<Integer> selectList = session.selectList("findUserIdsByProjectId", projectId);
		session.close();
		return selectList;
	}


	@Override
	public List<Integer> findProjectIdsByUserId(int userId) {
		SqlSession session = sqlSessionFactory.openSession();
		
		List<Integer> selectList = session.selectList("findProjectIdsByUserId", userId);
		session.close();
		return selectList;
	}
	
	

}
